package com.adminx.dao;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import com.adminx.entity.Authority;
import com.adminx.pojo.AuthorityPojo;
import com.adminx.util.PageResults;

@Repository
public class AuthorityDao extends BaseDao<Authority, Long>{
	
	protected static Logger LOGGER = Logger.getLogger(AuthorityDao.class); 
	
	/**
	 * 分页查找权限
	 * @param userName
	 * @return
	 */
	public PageResults<AuthorityPojo> query(Integer pageIndex, Integer pageSize, String searchText){
		String sql = 
			"SELECT " +
			"	t.authority_id id, " +
			"	t.authority_name authorityName, " +
			"	DATE_FORMAT( " +
			"		t.create_date, " +
			"		'%Y-%m-%d %H:%i:%S' " +
			"	) createDate, " +
			"	DATE_FORMAT( " +
			"		t.update_date, " +
			"		'%Y-%m-%d %H:%i:%S' " +
			"	) updateDate, " +
			"t.remark remark " +
			"FROM " +
			"	xx_authority t";
		String countSql = 
			"SELECT count(1) " +
			"FROM " +
			"	xx_authority t";
		
		if(!StringUtils.isBlank(searchText)){
			sql += " WHERE t.authority_name like '%" + searchText + "%' or t.remark like '%" + searchText + "%' ";
			countSql += " WHERE t.authority_name like '%" + searchText + "%' or t.remark like '%" + searchText + "%' ";
		}	
		return this.findPageByFetchedSql(AuthorityPojo.class, sql, countSql, pageIndex, pageSize);
	}

	/**
	 * 查询权限列表
	 * @param map
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<AuthorityPojo> findAuthorityList(Long roleID) {
		
		String sql = 
			"SELECT " +
			"	t.authority_id id, " +
			"	t.authority_name authorityName, " +
			"	t.authority_code authorityCode, " +
			"	DATE_FORMAT(t.create_date,'%Y-%m-%d %H:%i:%S') createDate, " +
			"	t.remark remark, " +
			"	DATE_FORMAT(t.update_date,'%Y-%m-%d %H:%i:%S') updateDate, " +
			"	r.role_id roleID, " +
			"	IFNULL(r.role_id, 0) hasAuthority " +
			"FROM " +
			"	xx_authority t " +
			"LEFT JOIN xx_role_authority r ON t.authority_id = r.authority_id " +
			"AND r.role_id =" + roleID;
		
		return this.getSession().createSQLQuery(sql).setResultTransformer(Transformers.aliasToBean(AuthorityPojo.class)).list();
	}
	
	/**
	 * 通过用户ID查询�?��权限
	 * @param map
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	public List<Authority> findAuthorityListByAccountID() {
		String sql = "SELECT t.authority_id,t.authority_name,t.authority_code,t.create_time,t.remark,t.update_time FROM xx_authority t";
		return this.getListBySQL(sql);
	}
	
	/**
	 * 通过ID删除权限
	 * @param authorityId
	 */
	public int delBatch(String ids) {
		
		String[] idsArray = ids.split(",");
		String hql = "";
		for (int i = 0; i < idsArray.length; i++) {
			if (i == 0) {
				hql = "authorityId=" + idsArray[i];
			} else {
				hql = hql + " or authorityId=" + idsArray[i];
			}
		}
		int updateCount = this.getSession().createQuery("DELETE FROM Authority WHERE " + hql).executeUpdate();
		return updateCount;
	}
	
	
	/**
	 * 通过权限查询�?��资源
	 * @return
	 */
	public StringBuilder getResourceByAuthority(Long authorityId){
		String sql = 
			"SELECT " +
			"	r.resource_id, " +
			"	r.resource_name, " +
			"	r.resource_path, " +
			"	r.resource_type, " +
			"	r.parent_id, " +
			"	r.resource_icon, " +
			"	CASE WHEN ar.id is NULL THEN " +
			"	'false' " +
			"ELSE " +
			"	'true' " +
			"END checked " +
			"FROM " +
			"	xx_resources r " +
			"LEFT JOIN xx_authority_resource ar ON r.resource_id = ar.resource_id " +
			"AND ar.authority_id = " + authorityId+
			" ORDER BY " +
			"	r.resource_order";
		@SuppressWarnings("unchecked")
		List<Object[]> queryList = this.getSession().createSQLQuery(sql).list();
		StringBuilder sb = new StringBuilder("[");
		for(int i = 0;i<queryList.size();i++){
			if(i<queryList.size()-1){
				sb.append("{\"id\":\""+queryList.get(i)[0]+"\",\"pId\":\""+queryList.get(i)[4]+"\",\"name\":\""+queryList.get(i)[1]+"\",\"checked\":\""+queryList.get(i)[6]+"\"},");
			}else{
				sb.append("{\"id\":\""+queryList.get(i)[0]+"\",\"pId\":\""+queryList.get(i)[4]+"\",\"name\":\""+queryList.get(i)[1]+"\",\"checked\":\""+queryList.get(i)[6]+"\"}");
			}
		}
		sb.append("]");
		return sb;
	}
}
